#!/usr/bin/env python
import base64, hashlib, os.path, os

class AlbumInfo(object):
    def __init__(self, name, params=None):
        self.name = name
        self.params = params

class LocalFile(object):
    def __init__(self, fn, filterOp=None):
        self.fn = fn
        self.b64fn = base64.b64encode(fn)
        self.filter = filterOp
        self.md5 = None
        self.album = None
        self.albumInfo = None

    def getData(self):
        fp = open(self.fn, 'r')
        data = fp.read()
        if self.filter:
            data = self.filter.filter(self.fn, data)
        fp.close()
        return data

    def calcHash(self):
        data = self.getData()
        if data is None:
            return None
        else:
            self.md5 = hashlib.md5(data).hexdigest()
            return self

    def __eq__(self, other):
        return self.md5 == other.md5

    def __hash__(self):
        return hash(self.md5)

    def __cmp__(self, other):
        return self.md5 < other.md5

    def __str__(self):
        return "LocalFile:" + self.fn

    def __repr__(self):
        return "LocalFile:" + self.fn

def main():
    import FileUtilities, re
    pattern = re.compile('.*\.(jpg|png|jpeg|tif|tiff|png|bmp|mpg|mov|avi)$', re.I)
    fns = FileUtilities.findFiles(['/home/sphinx/Pictures/volcano'], pattern)
    lfiles = [LocalFile(x) for x in fns]
    for lfile in lfiles:
        lfile.calcHash()
    lflen = len(lfiles)
    lfmid25 = int(lflen*.25)
    lfmid75 = int(lflen*.75)
    lf1 = lfiles[:lfmid75]
    lf2 = lfiles[lfmid25:]
    print lfmid25, lfmid75, lflen, len(lf1), len(lf2)
    lf1set = set(lf1)
    lf2set = set(lf2)
    print len(lf1set)
    print len(lf2set)
    print lf2set - lf1set
    iset = lf2set.intersection(lf1set)
    uset = lf2set.union(lf1set)
    print 'intersection:',len(iset)
    print 'union:',len(uset)
    

if __name__ == '__main__':
    main()
